unit Converter;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This file is part of the PasCalc Calculator

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This unit is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ComCtrls, StdCtrls, ExtCtrls, Conv;

type

  { TfrmValConverrter }

  TfrmValConverrter = class(TForm)
    btnConvert: TButton;
    btnValueFromEdField: TButton;
    btnAnswToEdField: TButton;
    btnAdd: TButton;
    cbCType: TComboBox;
    cbFrom: TComboBox;
    cbTo: TComboBox;
    cbCNVtype: TComboBox;
    edValue: TEdit;
    edTo: TEdit;
    lblCoefType: TLabel;
    edRoute: TLabeledEdit;
    edFrom: TLabeledEdit;
    edA_To: TLabeledEdit;
    edCoef: TLabeledEdit;
    lblCType: TLabel;
    lblFrom: TLabel;
    lbTo: TLabel;
    pcConverter: TPageControl;
    tValConv: TTabSheet;
    pAddCnv: TTabSheet;
    procedure btnAddClick(Sender: TObject);
    procedure btnAnswToEdFieldClick(Sender: TObject);
    procedure btnConvertClick(Sender: TObject);
    procedure btnValueFromEdFieldClick(Sender: TObject);
    procedure cbCTypeChange(Sender: TObject);
    procedure cbFromChange(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    procedure LangInit(Str: TStringList);
  end; 

var
  frmValConverrter: TfrmValConverrter;
  edExpr: TEdit;

implementation

{ TfrmValConverrter }

var
  Cnv: TConverter;

{ TfrmValConverrter }

procedure TfrmValConverrter.FormShow(Sender: TObject);
var
  i: integer;
begin
  Cnv := TConverter.Create;
  try
    Cnv.LoadFromFile('convs.dat');
  except
    Cnv.Free;
  end;
  cbFrom.Items.Clear;
  for I := 0 to cnv.GroupsCount - 1 do
    cbCType.Items.Add(ANSIToUtf8(Cnv.GroupName[i]));
  cbCType.ItemIndex := 0;
  cbCTypeChange(Self);
end;

procedure TfrmValConverrter.LangInit(Str: TStringList);
begin
  frmValConverrter.Caption := Str[64];
  lblCType.Caption := Str[65];
  lblFrom.Caption := Str[66];
  btnValueFromEdField.Caption := Str[67];
  lbTo.Caption := Str[68];
  btnConvert.Caption := Str[69];
  btnAnswToEdField.Caption := Str[70];
  tValConv.Caption := Str[71];
  pAddCnv.Caption := Str[72];
  edRoute.EditLabel.Caption := Str[73];
  edFrom.EditLabel.Caption := Str[74];
  edA_To.EditLabel.Caption := Str[75];
  edCoef.EditLabel.Caption := Str[76];
  cbCnvType.Items.Strings[0] := Str[77];
  cbCnvType.Items.Strings[1] := Str[78];
  cbCnvType.Items.Strings[2] := Str[79];
  cbCnvType.Items.Strings[3] := Str[80];
  lblCoefType.Caption := Str[81];
  btnAdd.Caption := Str[82];
end;

procedure TfrmValConverrter.FormClose(Sender: TObject;
  var CloseAction: TCloseAction);
begin
  FreeAndNil(Cnv);
end;

procedure TfrmValConverrter.cbCTypeChange(Sender: TObject);
var
  i: integer;
begin
  cbFrom.Items.Clear;
  for I := 0 to cnv.RoutesCount[cbCType.ItemIndex] - 1 do
    cbFrom.Items.Add(ANSIToUtf8(Cnv.RouteName[cbCType.ItemIndex, i]));
  cbFrom.ItemIndex := 0;
  cbFrom.OnChange(Self);
end;

procedure TfrmValConverrter.btnConvertClick(Sender: TObject);
var
  cnvr: TConversion;
begin
  cnvr := Cnv.Conversion[cbCType.ItemIndex, cbFrom.ItemIndex,
    cbTo.ItemIndex];
  try
    case Cnvr.CoefType of
      ctDiv: edTo.Text := FloatToStr(StrToFloat(edValue.Text) / cnvr.Coef);
      ctMult: edTo.Text := FloatToStr(StrToFloat(edValue.Text) * cnvr.Coef);
      ctAdd: edTo.Text := FloatToStr(StrToFloat(edValue.Text) + cnvr.Coef);
      ctMinus: edTo.Text := FloatToStr(StrToFloat(edValue.Text) - cnvr.Coef);
    end;
  except
    edTo.Text := 'Ошибка!'
  end;
end;

procedure TfrmValConverrter.btnValueFromEdFieldClick(Sender: TObject);
begin
  edValue.Text := edExpr.Text;
end;

procedure TfrmValConverrter.btnAnswToEdFieldClick(Sender: TObject);
begin
  if edTo.Text <> 'Ошибка!' then edExpr.Text := edTo.Text;
end;

procedure TfrmValConverrter.btnAddClick(Sender: TObject);
var
  a: array [0..3] of TCoefType = (ctMult, ctDiv, ctMinus, ctAdd);
begin
  try
    if Cnv <> nil then
      Cnv.Add(Utf8ToAnsi(edRoute.Text), Utf8ToAnsi(edFrom.Text), Utf8ToAnsi(edA_To.Text),
        StrToFloat(edCoef.Text), a[cbCnvType.ItemIndex]);
  except
  end;
  cnv.SaveToFile('convs.dat');
  Cnv.Free;
  FormShow(Self);
end;

procedure TfrmValConverrter.cbFromChange(Sender: TObject);
var
  i: integer;
begin
  cbTo.Items.Clear;
  for I := 0 to cnv.ConvsCount[cbCType.ItemIndex, cbFrom.ItemIndex] - 1 do
    cbTo.Items.Add(ANSIToUtf8(Cnv.Conversion[cbCType.ItemIndex, cbFrom.ItemIndex, i].Name));
  cbTo.ItemIndex := 0;
end;

initialization
  {$I Converter.lrs}

finalization
  if Cnv <> nil then FreeAndNil(Cnv);

end.

